package com.kingint.web.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * 日期工具类
 */
public class DateUtil {

    /**
     * 从Date类型的时间中提取日期部分
     */
    public static Date getDate(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND,0);
        return calendar.getTime();
    }

    /**
     * 从Date类型的时间中提取时间部分
     */
    public static Date getTime(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.YEAR, 1970);
        calendar.set(Calendar.MONTH, 0);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        return calendar.getTime();
    }

    public static String dateToStr(Date date, String formatStr) {
        SimpleDateFormat format = new SimpleDateFormat(formatStr);
        String s = "";

        try {
            s = format.format(date);
        } catch (Exception var5) {
            var5.printStackTrace();
        }

        return s;
    }
    public static Date strToDate(String str, String formatStr) {
        SimpleDateFormat format = new SimpleDateFormat(formatStr);
        Date date = null;

        try {
            date = format.parse(str);
        } catch (ParseException var5) {
            var5.printStackTrace();
        }

        return date;
    }

    /**
     * 获取明天的日期
     * @return
     */
    public static Date getTomorrowDate(){
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        c.add(Calendar.DAY_OF_MONTH, 1);
        String format = sf.format(c.getTime());
        Date parse=null;
        try {
            parse = sf.parse(format);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return parse;
    }

    /**
     * 根据日期获取 星期 （2019-05-06 ——> 星期一）
     *
     * @param datetime
     * @return
     */
    public static Integer dateToWeek(Date datetime) {
//        String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
        Calendar cal = Calendar.getInstance();
        cal.setTime(datetime);
        //一周的第几天
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0)
            w = 0;
        int weekNum = getWeekNum(w);
        return weekNum;
    }
    public static  int getWeekNum(int w){
        int num=0;
        switch (w){
            case 0:num=7;break;
            case 1:num=1;break;
            case 2:num=2;break;
            case 3:num=3;break;
            case 4:num=4;break;
            case 5:num=5;break;
            case 6:num=6;break;
        }
        return num;
    }

    /**
     * 时间添加天
     *
     * @param day
     * @return
     */
    public static Date addTimeByDay(Date date, Integer day) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, day);
        Date time = calendar.getTime();
        return time;
    }

    /**
     * 时间添加分钟
     *
     * @param minute
     * @return
     */
    public static Date addTimeMinute(Date date, Integer minute) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MINUTE, minute);
        Date time = calendar.getTime();
        return time;
    }
}
